﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Runtime.Serialization.Json;
using ISAMonitor.DNSPODAPI;
using ISAMonitor.RESTTool;

namespace ISAMonitor.DNSPODAPI
{
    /// <summary>
    /// 对DNSPOD的API进行封装
    /// </summary>
    public class DNSPodCommand
    {
        private readonly string username;
        private readonly string password;

        private string CommandInvoke(string pagename,string commandparam)
        {
            /*login_email - 用户帐号
 login_password - 用户密码
              format {json,xml} - 返回的数据格式，支持json和xml.*/

            StringBuilder mysb = new StringBuilder();
            mysb.Append("login_email=" + username +"&");
            mysb.Append("login_password=" + password + "&");
            mysb.Append("format=json"+"&");
            mysb.Append(commandparam);

            string ret = RESTCommand.SendCommand("http://www.dnspod.com"+pagename,mysb.ToString());
            return ret;
        }

        public DNSPodCommand(string username, string password)
        {
            this.username = username;
            this.password = password;
        }

        public DomainListRet GetDomainList()
        {
            string ret = CommandInvoke(@"/API/Domain.List","");
            var obj =  SerializationTool.GetObj<DomainListRet>(ret);
            return obj;
        }

        public RecordListRet GetRecordList(string domain_id)
        {
            string ret = CommandInvoke(@"/API/Record.List", "domain_id=" + domain_id);
            var obj = SerializationTool.GetObj<RecordListRet>(ret);
            return obj;
        }

        public RecordModifytRet ModifyRecord(string domain_id, string record_id, string sub_domain, string record_type, string record_line, string value, string mx, string ttl)
        {
            /*
 domain_id - 域名ID, 如 12345
 record_id - 记录ID, 如 1234567
 sub_domain - 主机记录, 如 www
 record_type {1,2,3,4,5,6,7} - 记录类型, 1:A, 2:CNAME, 3:MX, 4:URL Redirect, 5:NS, 6:TXT, 7:AAAA
 record_line {1,2,3,4} - 线路类型, 1:通用, 2:电信, 3:网通, 4:教育网
 value - 记录值, 如 IP:200.200.200.200, CNAME: cname.dnspod.com., MX: mail.dnspod.com.
 mx {1-20} - MX优先级, 当记录类型是 MX 时有效，范围1-20
 ttl {1-604800} - TTL，范围1-604800
             */

            //TODO 等待奶罩修改API实现后继续！！！

            StringBuilder mysb = new StringBuilder();

            mysb.Append("domain_id=" + domain_id + "&");
            mysb.Append("record_id=" + record_id + "&");
            mysb.Append("sub_domain=" + sub_domain + "&");
            mysb.Append("record_type=" + record_type + "&");
            mysb.Append("record_line=" + record_line + "&");
            mysb.Append("value=" + value + "&");
            mysb.Append("mx=" + mx + "&");
            mysb.Append("ttl=" + ttl + "&");

            string ret = CommandInvoke(@"/API/Record.Modify", mysb.ToString());
            var obj = SerializationTool.GetObj<RecordModifytRet>(ret);
            return obj;
        }
    }
}